package by.bsu.scs.presentation.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import by.bsu.scs.db.dao.LocalizationHome;
import by.bsu.scs.db.dao.UserHome;
import by.bsu.scs.model.Localization;
import by.bsu.scs.model.User;
import by.bsu.scs.presentation.form.ProfileForm;
import by.bsu.scs.security.MD5Encoder;
import by.bsu.scs.util.manager.SessionManager;

/**
 * @author Alexey Hursevich
 * @version 1.0
 * 
 * All actions connected with user profile editing.
 */
public class ProfileAction extends DispatchAction {
	
	private static final String EDIT = "edit";
	private static final String SAVE = "save";
	
	private static final String PARAM_LOCALE = "locale";
	
	private static final String STUDENT_MAIN = "student";
	private static final String INSTRUCTOR_MAIN = "instructor";
	private static final String ADMIN_MAIN = "admin";
	
	/**
	 * Retrieves user info from session and sets user login and
	 * password to profile table bean.
	 * This info will be used for validation when user will change
	 * the current password entering it for identity check.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		ProfileForm pForm = (ProfileForm) form;
		User user = SessionManager.getInstance().getUser(request);
		
		pForm.setCurrentLogin(user.getEmail());
		pForm.setCurrentPassword(user.getPassword());
		
		pForm.setOldPasswordCheck("");
		pForm.setNewPassword("");
		pForm.setNewPasswordConfirm("");
		
		return mapping.findForward(EDIT);
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		ProfileForm pForm = (ProfileForm) form;
		User user = SessionManager.getInstance().getUser(request);
		UserHome dao = new UserHome();
		
		String newPassword = MD5Encoder.getHash( pForm.getNewPassword() );
		user.setPassword(newPassword);
		dao.merge( user );
		
		return mapping.findForward(SAVE);
	}
	
	public ActionForward set_locale(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		String locale = request.getParameter(PARAM_LOCALE);
		Localization locModel = (Localization) new LocalizationHome().findByExample( new Localization( locale ) ).get(0);
		User userModel = SessionManager.getInstance().getUser(request);
		UserHome dao = new UserHome();
		
		userModel.setLocalization( locModel );
		dao.merge( userModel );
		
		request.getSession().setAttribute(PARAM_LOCALE, locale);
		request.getSession().setAttribute( "org.apache.struts.action.LOCALE", new Locale( locale ) );
		
		String userRole = userModel.getUserRole().getRoleName();
		String forward = null;
		if (STUDENT_MAIN.equalsIgnoreCase(userRole)) {
			forward = STUDENT_MAIN;
		} else if (INSTRUCTOR_MAIN.equalsIgnoreCase(userRole)) {
			forward = INSTRUCTOR_MAIN;
		} else if (ADMIN_MAIN.equalsIgnoreCase(userRole)) {
			forward = ADMIN_MAIN;
		}	
		
		return mapping.findForward(forward);
	}
}
